#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#


"""This module provides an interface to fetch data from GAE db.

The data from tsv files (metadata and global data) is dumped into the GAE db as
a first step in the workflow. The functions defined in this class are used to
fetch this data while generating the website.
"""

import constants
from db_utils import GlobalData
from db_utils import Metadata
from db_utils import Page


class GQLUtils(object):
  """This is a utility class to fetch data from GAE db.

  The data from tsv files is dumped into GAE db. The functions defined in this
  class are used to fetch the data while generating the page tree.
  """

  def __init__(self, version_id):
    """The constructor.

    Args:
      version_id: The current version number of the website.
    """
    self.version_id = version_id

  def IsHeaderRow(self, row_id):
    """Checks whether the given row is a header row.

    Args:
      row_id: the row number to check.

    Returns:
      True if the row id is 0, false otherwise.
    """
    return row_id <= 0

  def GetGlobalRowByNumber(self, row_no):
    """Fetches the given row from GlobalData table.

    Args:
      row_no: The row number to fetch.

    Returns:
      A list of all the fields in the given row in the GlobalData table.
    """
    query = GlobalData.gql('WHERE row_id=:1 AND version_id=:2', row_no,
                           self.version_id)
    fields = []
    result = query.get()
    if result:
      fields.append(result.value)
      fields.append(result.variable)
    return fields

  def GetMetadataRowByNumber(self, row_no):
    """Fetches the given row from Metadata table.

    Args:
      row_no: The row number to fetch.

    Returns:
      A list of all the fields in the given row in the Metadata table.
    """
    query = Metadata.gql('WHERE row_id=:1 AND version_id=:2', row_no,
                         self.version_id)
    fields = []
    result = query.get()
    if result:
      fields.append(result.id)
      fields.append(result.sorting_column1)
      fields.append(result.sorting_column2)
      fields.append(result.sorting_column3)
      fields.append(result.title)
      rest = result.rest_metadata.split(constants.FIELD_SEPARATOR)
      for r in rest:
        fields.append(r)
    return fields

  def GetAllRows(self):
    """Fetch all rows from Metadata table.

    Returns:
      A list of all the rows in Metadata table.
    """
    query = Metadata.gql('WHERE version_id=:1', self.version_id)
    all_rows = []
    for result in query:
      if not self.IsHeaderRow(result.row_id):
        each_row = []
        each_row.append(result.id)
        each_row.append(result.sorting_column1)
        each_row.append(result.sorting_column2)
        each_row.append(result.sorting_column3)
        each_row.append(result.title)
        rest = result.rest_metadata.split(constants.FIELD_SEPARATOR)
        for r in rest:
          each_row.append(r)
        all_rows.append(each_row)
    return all_rows

  def GetAllUrls(self):
    """Fetch all urls from Page table.

    Returns:
      A list of all the urls in the Page table.
    """
    query = Page.gql('WHERE version_id=:1', self.version_id)
    urls = []
    for result in query:
      urls.append(result.html_url)
    return urls
